class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def findMode(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        out = []
        max_val = -1
        stack = [root]
        while stack[-1].left:
            stack.append(stack[-1].left)
        prev = None
        count = 0
        while stack:
            node = stack.pop()
            if not prev:
                prev = node
                count = 1
            else:
                if node.val == prev.val:
                    count += 1
                else:
                    count = 1
                prev = node
            if count == max_val:
                out.append(node.val)
            else:
                if count > max_val:
                    max_val = count
                    out = [node.val]
            if node.right:
                node = node.right
                while node:
                    stack.append(node)
                    node = node.left
        return out

